package com.example.demo.producer.receiver;

import com.example.demo.common.domain.ChannelRead;
import com.example.demo.producer.service.ActiveResService;
import com.example.demo.producer.service.ServerFingerService;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Auther Created by l.wang on 2019/4/3.
 */
@Component
public class ActiveMqListener {

    private ActiveResService activeResService;

    public final String thisFinger;

    private ExecutorService service = Executors.newCachedThreadPool();

    private Executor getExecutors(){
        return service;
    }

    @Autowired
    public ActiveMqListener(ServerFingerService serverFingerService, ActiveResService activeResService) {
        this.thisFinger = serverFingerService.getServerFinger();
        this.activeResService = activeResService;
    }

    @Bean
    public Queue connectChannelReadQueue() {
        return new Queue("queue.connectServices:" + thisFinger, true, false, true);
    }

    @JmsListener(destination = "#{'queue.connectServices:' + activeMqListener.thisFinger}")
    public void receiver(ChannelRead str){
        System.out.println("接收："+ str.toString());
        service.execute(()->activeResService.dispose(str));
    }


}
